<template>
  <div id="artList">
    <van-pull-refresh
      v-model="refreshLoading"
      :success-text="successText"
      :success-duration="1200"
      @refresh="onRefresh"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :offset="100"
        @load="onLoad"
      >
        <ArtItem v-for="(item, index) in list" :key="index" :articles="item" />
        <!-- <van-cell v-for="item in list" :key="item.id" :title="item.title" /> -->
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
import { getArtChannels } from '@/api/channel'
import ArtItem from '@/components/art-item/'
export default {
  name: 'artList',
  data () {
    return {
      list: [],
      loading: false, // 控制加载中的loading状态
      finished: false, // 控制是否继续加载
      page: 1,
      refreshLoading: false,
      successText: null
    }
  },
  created () {},
  methods: {
    // 获取文章列表
    async onLoad () {
      // 异步更新数据
      const { data } = await getArtChannels({
        channel_id: this.channel.id,
        page: this.page
      })
      this.loading = false
      if (data.data[0]) {
        this.list.push(...data.data)
        this.page = data.page
      } else {
        this.finished = true
      }
    },
    // 上拉刷新
    async onRefresh () {
      const { data } = await getArtChannels({
        channel_id: this.channel.id,
        page: this.page
      })
      this.refreshLoading = false
      if (data.data[0]) {
        this.page = data.page
        this.list.unshift(...data.data)
      } else {
        this.finished = true
      }
      this.successText = `添加${data.data.length}条内容`
    }
  },
  components: { ArtItem },
  props: {
    channel: {
      type: Object,
      required: true
    }
  },
  watch: {},
  computed: {},
  mounted () {}
}
</script>

<style lang="less" scoped>
#artList {
  position: fixed;
  left: 0;
  right: 0;
  top: 90px;
  bottom: 50px;
  overflow-y: auto;
}
</style>
